<template>
  <div class="login">
    <div class="login_title">
      <p>
        <img src="./title.png" />
      </p>
    </div>
    <div class="login_main">
      <div class="main_left"></div>
      <div class="main_right">
        <div class="right_title">用户登录</div>
        <form action>
          <div class="username">
            <img src="./username.png" alt />
            <input type="text" v-model="username" placeholder="请输入用户名" />
          </div>
          <div class="password">
            <img src="./password.png" alt />
            <input type="password" v-model="password" placeholder="请输入密码" />
          </div>
          <div class="code">
            <img src="./code.png" alt />
            <input type="text" placeholder="请输入验证码" v-model="inputcode" />
            <div class="code_img">
              <el-button type="danger" @click="createCode" class="verification">{{checkCode}}</el-button>
            </div>
          </div>
          <div class="yes_login">
            <a @click="login">登&nbsp;&nbsp;&nbsp;&nbsp;录</a>
          </div>
          <div class="yes_login">
            <a @click="reg">注&nbsp;&nbsp;&nbsp;&nbsp;册</a>
          </div>
        </form>
        <div></div>
      </div>
    </div>
  </div>
</template>

<script>
import API from "@/api/user.js";
export default {
  name: "login",
  data() {
    return {
      username: "",
      password: "",
      acctNum: "",
      pwd: "",
      code: "",
      checkCode: "",
      picLyanzhengma: "",
      inputcode: ""
    };
  },
  mounted() {
    this.createCode();
  },
  activated: function() {
    this.createCode();
  },
  methods: {
    createCode() {
      //先清空验证码的输入
      this.code = "";
      this.checkCode = "";
      this.picLyanzhengma = "";
      //验证码的长度
      var codeLength = 4;
      //随机数
      var random = new Array(
        0,
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        "A",
        "B",
        "C",
        "D",
        "E",
        "F",
        "G",
        "H",
        "I",
        "J",
        "K",
        "L",
        "M",
        "N",
        "O",
        "P",
        "Q",
        "R",
        "S",
        "T",
        "U",
        "V",
        "W",
        "X",
        "Y",
        "Z"
      );
      for (var i = 0; i < codeLength; i++) {
        //取得随机数的索引（0~35）
        var index = Math.floor(Math.random() * 36);
        //根据索引取得随机数加到code上
        this.code += random[index];
      }
      //把code值赋给验证码
      this.checkCode = this.code;
    },
    reg() {
      this.$router.push("/reg");
    },
    login() {
      if (this.inputcode != this.code) {
        this.$message.error("验证码不正确");
        this.createCode();
      } else {
        let obj = {
          username: this.username,
          password: this.password
        };
        this.$axios.post(API.login, obj).then(res => {
          if (res.data.responseCode === 200) {
            sessionStorage.setItem("userName", res.data.data.userName);
            sessionStorage.setItem("userCode", res.data.data.userCode);
            sessionStorage.setItem("userUrl", res.data.data.userUrl);
            this.$router.push({
              path: "/tu1",
              name: "tu1",
              query: {
                user: this.user
              }
            });
          } else {
            this.$message.error("登陆失败！");
          }
        });
      }
    }
  }
};
</script>
<style scoped>
fieldset,
img,
input,
button {
  border: none;
  padding: 0;
  margin: 0;
  outline-style: none; /*定义无轮廓*/
}
ul,
ol {
  list-style: none;
}
input {
  padding-top: 0;
  padding-bottom: 0;
}
select,
input {
  vertical-align: middle;
}
select,
input,
textarea {
  font-size: 14px;
  margin: 0;
}
textarea {
  resize: none; /*用户无法调整元素的尺寸*/
}
img {
  border: 0;
  vertical-align: middle; /*去除图片下间隙*/
}
table {
  border-collapse: collapse; /*合并外边线*/
}
body {
  font-family: Microsoft YaHei, Arial, "\5b8b\4f53";
  background: #0058ac;
}
.clearfix:before,
.clearfix:after {
  content: "";
  display: table;
}
.clearfix:after {
  clear: both;
}
.clearfix {
  *zoom: 1;
}
h1,
h2,
h3,
h4,
h5,
h6 {
  text-decoration: none;
  font-weight: normal;
  font-size: 100%;
}
.login {
  width: 1000px;
  height: 500px;
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: -500px;
  margin-top: -350px;
}
.login > .login_title {
  height: 80px;
  text-align: center;
  margin-top: 0px;
}
.login > .login_title > p {
  color: #fff;
  font-size: 40px;
  font-weight: 600;
  letter-spacing: 5px;
}
.login > .login_main {
  width: 800px;
  height: 320px;
  margin-top: 100px;
  margin-left: 100px;
}
.login > .login_main > .main_left {
  height: 320px;
  width: 450px;
  float: left;
  -moz-border-top-left-radius: 10px;
  -webkit-border-top-left-radius: 10px;
  border-top-left-radius: 10px;
  -moz-border-bottom-left-radius: 10px;
  -webkit-border-bottom-left-radius: 10px;
  border-bottom-left-radius: 10px;
  background: url("./login_left.jpg") no-repeat;
}
.login > .login_main > .main_right {
  height: 320px;
  width: 350px;
  float: right;
  background-color: rgba(58, 62, 65, 0.8);
  -moz-border-top-right-radius: 10px;
  -webkit-border-top-right-radius: 10px;
  border-top-right-radius: 10px;
  -moz-border-bottom-right-radius: 10px;
  -webkit-border-bottom-right-radius: 10px;
  border-bottom-right-radius: 10px;
}
.login > .login_main > .main_right > .right_title {
  color: #fff;
  font-size: 20px;
  font-weight: 700;
  text-align: center;
  line-height: 60px;
  background: url("./border_img.png") repeat;
  margin-bottom: 25px;
}
.login > .login_main > .main_right > form > div {
  position: relative;
}
.login > .login_main > .main_right > form > div > img {
  position: absolute;
  left: 62px;
  top: 7px;
}
.login > .login_main > .main_right > form input {
  height: 35px;
  width: 250px;
  border: 1px solid #d3d8dc;
  box-sizing: border-box;
  margin-bottom: 20px;
  -moz-border-radius: 3px;
  -webkit-border-radius: 3px;
  border-radius: 3px;
  color: #333;
  padding-left: 40px;
}
.login > .login_main > .main_right > form > .code > input {
  width: 150px;
  margin-left: 40px;
}
.login > .login_main > .main_right > form > .code > .code_img {
  width: 90px;
  height: 35px;
  float: right;
  margin-right: 50px;
  cursor: pointer;
}
.login > .login_main > .main_right > form input:focus {
  border: 1px solid #999;
}
.login > .login_main > .main_right > form > .yes_login > a {
  display: block;
  width: 120px;
  height: 35px;
  line-height: 35px;
  text-align: center;
  background-color: #0064c8;
  -moz-border-radius: 3px;
  -webkit-border-radius: 3px;
  border-radius: 3px;
  color: #fff;
  text-decoration: none;
  margin-left: 35px;
  float: left;
  cursor: pointer;
}
.login > .login_main > .main_right > form > .yes_login > a:hover {
  background-color: #0058af;
}
.verification {
  border-radius: 12px;
  width: 70px;
  margin-left: 30px;
  letter-spacing: 5px;
  height: 35px;
  transform: translate(-15px, 0);
}
.captcha {
  height: 50px;
  text-align: justify;
}
@import url(reset.css);
</style>
<style >
#app {
  margin: 0;
  padding: 0;
}
body {
  background-color: rgba(6, 240, 240, 0.493);
  background-image: url(bgImg1.png);
  background-size: 100%;
  background-repeat: no-repeat;
}
</style>